package bunny.demo.shake;

import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;
import android.util.Log;

public class Shaker implements SensorEventListener
{
	private static final String TAG = "Shaker";
	private static final int UPDATE_INTERVAL = 100;
	private static final int shakeThreshold = 400;
	// private Context context;
	private SensorManager sensorManager;
	private ShakerEventListenr shakerEventListenr;
	private Sensor sensor;
	private boolean isRun = false;
	long lastUpdateTime = 0;
	private float lastX = 0;
	private float lastY = 0;
	private float lastZ = 0;

	private float maxDelta;

	public void setShakerEventListenr(ShakerEventListenr shakerEventListenr)
	{
		this.shakerEventListenr = shakerEventListenr;
	}

	public Shaker(Context context)
	{
		// this.context = context;
		sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

	}

	private void registLinearSensor()
	{
		sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
		sensorManager.registerListener(	this,
										sensor,
										SensorManager.SENSOR_DELAY_NORMAL);
	}

	private void unregistSensor()
	{
		if (sensorManager != null)
		{
			sensorManager.unregisterListener(this);
		}
	}

	public void start()
	{
		Log.i(	TAG,
				"开始检测摇晃");
		isRun = true;
		registLinearSensor();
	}

	public void stop()
	{
		Log.i(	TAG,
				"停止检测摇晃");
		isRun = false;
		unregistSensor();
		lastX = 0;
		lastY = 0;
		lastZ = 0;
		shakerEventListenr.OnShake("");
	}

	public void showAllSensors()
	{
		List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
		for (Sensor sensor : sensors)
		{
			Log.i(	TAG,
					"name--" + sensor.getName());
			Log.i(	TAG,
					"  type--" + getSensorType(sensor.getType()));
		}
	}

	private String getSensorType(int type)
	{
		switch (type)
		{
			case Sensor.TYPE_ACCELEROMETER:
				return "TYPE_ACCELEROMETER";
			case Sensor.TYPE_GRAVITY:
				return "TYPE_GRAVITY";
			case Sensor.TYPE_GYROSCOPE:
				return "TYPE_GYROSCOPE";
			case Sensor.TYPE_LIGHT:
				return "TYPE_LIGHT";
			case Sensor.TYPE_LINEAR_ACCELERATION:
				return "TYPE_LINEAR_ACCELERATION";
			case Sensor.TYPE_MAGNETIC_FIELD:
				return "TYPE_MAGNETIC_FIELD";
			case Sensor.TYPE_ORIENTATION:
				return "TYPE_ORIENTATION";
			case Sensor.TYPE_PRESSURE:
				return "TYPE_PRESSURE";
			case Sensor.TYPE_PROXIMITY:
				return "TYPE_PROXIMITY";
			case Sensor.TYPE_ROTATION_VECTOR:
				return "TYPE_ROTATION_VECTOR";
			case Sensor.TYPE_TEMPERATURE:
				return "TYPE_TEMPERATURE";

			default:
				return null;
		}
	}

	@Override
	public void onAccuracyChanged(	Sensor sensor,
									int accuracy)
	{
	}

	@Override
	public void onSensorChanged(SensorEvent event)
	{
		if (isRun)
		{
			long currentTime = System.currentTimeMillis();
			long diffTime = currentTime - lastUpdateTime;

			if (diffTime < UPDATE_INTERVAL)
			{
				return;
			}

			lastUpdateTime = currentTime;
			float x = event.values[SensorManager.DATA_X];
			float y = event.values[SensorManager.DATA_Y];
			float z = event.values[SensorManager.DATA_Z];

			float deltaX = x - lastX;
			float deltaY = y - lastY;
			float deltaZ = z - lastZ;

			lastX = x;
			lastY = y;
			lastZ = z;

			float delta = FloatMath.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / diffTime * 10000;
			StringBuilder sb = new StringBuilder();
			sb.append("X轴最大正向加速度: " + lastY);
			sb.append("\nX轴最大负向加速度: " + lastX);
			sb.append("\nX轴当前加速度       : " + x);
			Log.i(	TAG,
					sb.toString());
			Log.i(	TAG,
					"delta-->" + delta);

			if (delta > maxDelta)
			{
				maxDelta = delta;

			}
			Log.e(	TAG,
					"maxDelta-->" + maxDelta);
			if (delta > shakeThreshold)
			{
				shakerEventListenr.OnShake("检测到摇晃");
			}
		}
	}
}